/*
 * @Author: rxn 1003312535@qq.com
 * @Date: 2024-06-19 16:23:32
 * @LastEditors: rxn 1003312535@qq.com
 * @LastEditTime: 2024-10-09 15:17:21
 * @FilePath: \vue3-progect\src\store\modules\menu.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import menuList from "@/router/menu/index.ts";
import { homeRouter } from "@/router/modules/home";
import { defineStore } from "pinia";

import { APP_ALL_MENU_LIST, APP_ASIDE_MENU_LIST } from "@/enums/cacheEnum";

import { createLocalStorage } from "@/utils/cache";
let ls = createLocalStorage();

export const useMenuStore = defineStore("app_menu", {
	state: () => ({
		//是否首次添加
		isFirstAdd: true,
		//后端返回的菜单列表
		menu: [],
		//左侧菜单列表
		asideMenuList: ls.getItem(APP_ASIDE_MENU_LIST) || []
	}),
	getters: {
		getAllMenu(state): Object[] {
			return state.menu.length
				? state.menu
				: ls.getItem(APP_ALL_MENU_LIST) || [];
		}
	},
	actions: {
		//获取 全部菜单列表
		async getMenuList() {
			this.setMenuList([...menuList]);
		},
		//设置 全部菜单列表
		setMenuList(data) {
			this.menu = data;
			ls.setItem(APP_ALL_MENU_LIST, this.getAllMenu);
		},
		//获取 侧边菜单
		getAsideMenuList(payload) {
			let currentMenu = this.getAllMenu.find(
				(it) => it.menuId == payload.menuId
			) || {
				children: []
			};
			this.setAsideMenuList(currentMenu.children);
		},
		//设置 侧边菜单
		setAsideMenuList(asideMenuList) {
			this.asideMenuList = asideMenuList;
			ls.setItem(APP_ASIDE_MENU_LIST, this.asideMenuList);
		}
	}
});
